/* 算法实现模板 */
import AlgorithmBase from "./AlgorithmBase";


class BFGS extends AlgorithmBase{
  constructor(){
    super('BFGS');
    this.descriptionUrl = `${this.homepage}/data/slm/ch6/READEME6.2.md`;
    this.modelUrl= `${this.homepage}/data/slm/ch6/6.2.md`;
  }

  init(){
    this.reset();
    this.initTrainDS();
  }



  reset(){
    this.dataset = undefined;
    this.trainDS = undefined;
    this.clear();
    this.initModel();
    this.initDescription();
    this.updateModel();
  }


  initDescription(){
    fetch(this.descriptionUrl ).then((response)=>{
      return response.text();
    }).then((txt)=>{

      this.reandMD(txt, this.descriptionDiv)
    })
  }

  
  initModel(){
    fetch(this.modelUrl).then((response)=>{
      return response.text();
    }).then((txt)=>{

      this.reandMD(txt, this.modelDiv)
    })

  }

  
  initTrainDS(){
    this.prepare(this.datasetUrl);
  }

  prepare(url){
    fetch(url).then((response)=>{
      return response.json();
    }).then((json)=>{
      this.trainDS = this.loadModel(json);
      this.updateModel();
    })
  }

  openFileSelector(){
    this.fileSelector.click();
  }

  // 获取文件路径加载
  handleFileChange(event){
    const files = event.target.files;

    for(let i = 0, il = files.length; i < il; i++){
      let reader = new FileReader();
      reader.onload = (e) => {      
        this.prepare(e.target.result);
        if(i === files.length - 1){
          event.target.value = '';
        }
      };
      reader.readAsDataURL(files[i])
    }
  }



}


export default BFGS;